<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>最小WebSocket测试</title>
</head>
<body>
    <h2>最小WebSocket测试</h2>
    <p>点击下面的按钮测试WebSocket连接</p>
    
    <div>
        <button onclick="testEcho()">测试Echo</button>
        <button onclick="testRaw()">测试RawWS</button>
    </div>
    
    <pre id="output" style="border:1px solid #ccc; padding:10px; height:300px; overflow:auto;"></pre>
    
    <script>
        var output = document.getElementById('output');
        
        function log(msg) {
            output.textContent += new Date().toLocaleTimeString() + ': ' + msg + '\n';
            output.scrollTop = output.scrollHeight;
        }
        
        function testEcho() {
            log('测试Echo WebSocket连接...');
            try {
                var ws = new WebSocket('ws://' + location.host + '/echo');
                
                ws.onopen = function() {
                    log('Echo连接成功!');
                    ws.send('Hello Echo');
                };
                
                ws.onmessage = function(e) {
                    log('收到Echo消息: ' + e.data);
                    // 测试完成后关闭
                    setTimeout(function() {
                        ws.close();
                    }, 1000);
                };
                
                ws.onclose = function(e) {
                    log('Echo连接关闭: ' + e.code);
                };
                
                ws.onerror = function(e) {
                    log('Echo连接错误!');
                    console.error(e);
                };
            } catch(e) {
                log('创建Echo连接失败: ' + e);
            }
        }
        
        function testRaw() {
            log('测试Raw WebSocket连接...');
            try {
                var ws = new WebSocket('ws://' + location.host + '/wsraw');
                
                ws.onopen = function() {
                    log('Raw连接成功!');
                    ws.send(JSON.stringify({
                        action: 'test',
                        customerId: 'test001',
                        timestamp: new Date().getTime()
                    }));
                };
                
                ws.onmessage = function(e) {
                    log('收到Raw消息: ' + e.data);
                    // 测试完成后关闭
                    setTimeout(function() {
                        ws.close();
                    }, 1000);
                };
                
                ws.onclose = function(e) {
                    log('Raw连接关闭: ' + e.code);
                };
                
                ws.onerror = function(e) {
                    log('Raw连接错误!');
                    console.error(e);
                };
            } catch(e) {
                log('创建Raw连接失败: ' + e);
            }
        }
    </script>
</body>
</html> 